#include "widget.h"
#include "ui_widget.h"

#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //QTimer递增测试
    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,&Widget::handle_QTimer);

    //timeEvent递减测试
    //先初始化timerId
    timerId=0;
}

Widget::~Widget()
{
    delete ui;
}

//QTimer
void Widget::on_pushButton_add_clicked()
{
    //按下按钮，QTimer开始计时
    //每1S，QTimer触发一次timeout信息
    timer->start(1000);
}
void Widget::handle_QTimer()
{
    int value=ui->lcdNumber_add->intValue();
    value++;
    ui->lcdNumber_add->display(QString::number(value));
}
void Widget::on_pushButton_add_2_clicked()
{
    //点击停止，timer停下
    timer->stop();
}


//timeEvent
void Widget::on_pushButton_minus_clicked()
{
    timerId=startTimer(1000);
}

void Widget::on_pushButton_minus_2_clicked()
{
    killTimer(timerId);
}

void Widget::timerEvent(QTimerEvent *event)
{
    //timeId不正确，就返回
    if(timerId!=event->timerId())
    {
        return;
    }
    //因为这个timeEvent事件每1s触发一次，所以实现每秒递减1
    int value=ui->lcdNumber_minus->intValue();
    if(value==0)
        return;
    value--;
    ui->lcdNumber_minus->display(QString::number(value));
}
